package com.wei.duowan.bbs.ui.fragment;

import android.content.Intent;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.NewsChannelBean;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.activity.NewsChannelActivity;
import com.wei.duowan.bbs.ui.activity.SearchActivity;
import com.wei.duowan.bbs.ui.adapter.NewsTitleAdapter;
import com.wei.duowan.bbs.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：赵若位
 * 时间：2019/8/1 15:11
 * 联系方式：1070138445
 * 功能：首页
 */
public class NewsFragment extends BaseFragment
{
    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.viewPager)
    ViewPager mViewPager;

    private NewsTitleAdapter mAdapter;

    private List<NewsChannelBean> mList;

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView()
    {
        mViewPager.setAdapter(mAdapter = new NewsTitleAdapter(getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);


        String json = StringUtils.getAssetsJson("头条标题-1.json");
        Gson gson = new Gson();
        mList = gson.fromJson(json, new TypeToken<ArrayList<NewsChannelBean>>()
        {
        }.getType());
        mViewPager.setOffscreenPageLimit(mList.size());
        mAdapter.setNewData(mList);
    }


    @OnClick({R.id.tv_search, R.id.img_edit})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.tv_search:
                mActivity.openActivity(SearchActivity.class);
                break;
            case R.id.img_edit:
                Intent it = new Intent(mActivity, NewsChannelActivity.class);
                it.putParcelableArrayListExtra(NewsChannelActivity.TAG, (ArrayList<? extends Parcelable>) mList);
                startActivity(it);
                break;
        }
    }
}
